package com.crm.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class Servlet extends HttpServlet {

	private static final long serialVersionUID = 1920451507718814747L;
	
	private static Logger logger = Logger.getLogger(Servlet.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String requestName = request.getParameter("method");
	    if(requestName == null || requestName.trim().equals(""))
	    	logger.error("The parameter [method] cannot be null.");
	    try {
			Method method = getClass().getDeclaredMethod(requestName, HttpServletRequest.class , HttpServletResponse.class);
			method.invoke(this, request , response);
		} catch (SecurityException e) {
			logger.error("You do not have permission:", e);
		} catch (NoSuchMethodException e) {
			logger.error("Methgod [" + requestName + "] not exist:", e);
		} catch (IllegalArgumentException e) {//
			logger.error("You passed an error parameter:", e);
		} catch (IllegalAccessException e) {
			logger.error("You do not access permission:", e);
		} catch (InvocationTargetException e) {
			logger.error("System has invocation target exception:", e);
		}
	}

}
